package com.aboo.redis.service;

import com.aboo.frame.util.redis.RedisClient;
import com.aboo.redis.domain.po.Cell;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

@Slf4j
@Service
public class RedisService {

    @Resource
    private RedisClient redisClient;

    public String getSetRedisStr() {
        String redisKey = "testKey";
        String redisVal = "testVal";
        redisClient.set(redisKey, redisVal, 80);
        String data = redisClient.get(redisKey);

        return data;
    }

    public Cell getSetRedisObj() {
        Cell org = new Cell("008", 18, new BigDecimal("25.65"));
        redisClient.setObj("testObjKey", org, 80);
        Cell sen = (Cell) redisClient.getObj("testObjKey");

        return sen;
    }

    public String incrRedis() {
        String redisKey = "incrKey";
        long redisVal = redisClient.incr(redisKey, 1);

        return String.valueOf(redisVal);
    }

    public String scoreRedis() {
        Set<ZSetOperations.TypedTuple<String>> tuples = new HashSet<>();
        tuples.add(new DefaultTypedTuple<>("AAA01", 0.1d));
        tuples.add(new DefaultTypedTuple<>("BBB02", 0.2d));
        tuples.add(new DefaultTypedTuple<>("CCC03", 0.3d));
        tuples.add(new DefaultTypedTuple<>("DDD04", 0.4d));
        tuples.add(new DefaultTypedTuple<>("EEE05", 0.5d));
        tuples.add(new DefaultTypedTuple<>("FFF06", 0.6d));
        redisClient.zadd("zaddtest", tuples);

        Set<ZSetOperations.TypedTuple<String>> set = redisClient.rangeWithScores("zaddtest", -3, -1);
        if (set != null) {
            set.forEach(a -> {
                log.info("value:{} --> score:{}", a.getValue(), a.getScore());
            });
        }
        return "OK";
    }

}
